﻿Imports System.Data.Objects

Public Class SiteDAO
    Inherits EntityDAO

    Function getAllSite() As List(Of Site)

        Try
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            Return ctx.Site().ToList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function
    Function addSite(ByVal site As Site) As Site
        Try
            Dim conStr As String = getConnectionString(site.CODSITE, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            ctx.AddToSite(site)
            ctx.SaveChanges()
            Return site
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function

    Function getSiteDefaultLayers() As List(Of LayersSiteConfig)
        Try
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            Return ctx.LayersSiteConfig().ToList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function

    Sub addConfigLayer(ByVal newConfigLayer As LayerConfig)
        Try
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            Dim confLayerEntity As LayersSiteConfig = LayersSiteConfig.CreateLayersSiteConfig(newConfigLayer.LayerName, newConfigLayer.LayerLabel, newConfigLayer.LayerActivated)
            ctx.AddToLayersSiteConfig(confLayerEntity)
            ctx.SaveChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub updateConfigLayer(ByVal layerConfig As LayerConfig)
        Try
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            Dim confLayerEntity As LayersSiteConfig = New LayersSiteConfig()
            confLayerEntity.layerName = layerConfig.LayerName
            confLayerEntity.layerLabel = layerConfig.LayerLabel
            confLayerEntity.layerActif = layerConfig.LayerActivated
            confLayerEntity.layerForm = layerConfig.LayerForm
            confLayerEntity.layerTable = layerConfig.LayerTable
            ctx.LayersSiteConfig.Attach(confLayerEntity)
            ctx.ObjectStateManager.ChangeObjectState(confLayerEntity, System.Data.EntityState.Modified)
            ctx.SaveChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub deleteConfigLayer(ByVal layerConfig As LayerConfig)
        Try
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            Dim confLayerEntity As LayersSiteConfig = New LayersSiteConfig()
            confLayerEntity.layerName = layerConfig.LayerName
            confLayerEntity.layerLabel = layerConfig.LayerLabel
            confLayerEntity.layerActif = layerConfig.LayerActivated
            ctx.LayersSiteConfig.Attach(confLayerEntity)
            ctx.ObjectStateManager.ChangeObjectState(confLayerEntity, System.Data.EntityState.Deleted)
            ctx.SaveChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub addSiteAppelations(ByVal site As Site, ByVal siteAppelations As System.ComponentModel.BindingList(Of SiteAppelation))
        Try
            If site Is Nothing OrElse siteAppelations Is Nothing Then
                Return
            End If
            Dim conStr As String = getConnectionString(Nothing, DatabaseType.TYPE_GLOBAL)
            Dim ctx As silm_global = New silm_global(conStr)
            For Each SiteApp As SiteAppelation In siteAppelations
                SiteApp.ID_Sit = site.ID_Sit
                ctx.AddToSiteAppelation(SiteApp)
                ctx.SaveChanges()
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

End Class
